import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用
            /**
             * 第一个参数Subject：商品名称
             * 第二个参数outTradeNo：编号 - 使用支付编号，业务比较简单，也可以直接使用订单编号
             * 第三个参数totalAmount：支付的总金额
             * 第四个参数resultUrl: 用户成功支付之后，跳转的页面
             */
            AlipayTradePagePayResponse response = Payment.Page()
                    .pay("Apple iPhone11 128G", "2234567890", "5799.00", "");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response.body);
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https"; // 协议
        config.gatewayHost = "openapi.alipaydev.com/gateway.do"; // 网关地址
        config.signType = "RSA2";
        config.appId = "2021000118602798";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        // 注意这个私钥，是自己的私钥
        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDGPr44vXQQm/UMD6jdonIOvFfyoq60ZvWLT2SAQ9M/qBk7pGzS8uxMe4Zy+Ydy/J/mmYXoVuZLC1sRJBgd0X+uhACVePF+5Fo5MozZaDBgd+k8UQQsHe4tf8PrAzGTsQZPtRrx2mfLXTOvXGTfKLus04+1uP/IK15yYMgXFz5NcFV2yExPIduPUn30E3bnPR2eDIS9rwX2Bf2FclgsG1b210eo/ghGkJfQ/mkzcBqCjAeG7O4nE9ENNg32mHUwpgZqv6X4wVKj0XI+S47/B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jz2XI+lGMW8JOf3iOWS2zYGG4eMim+TDpIJ5pCW/RAoGBAMC2m6X1NHzHKS+xTd1jaF9a7h7McMUOVgK3FuZ5vqkNQSU6Mvmky0dR4jw0dZBdtWcZcDtYqNtjoZWcmOUWe6Nd3upKXt4mFA+eCetnsKao+wZVJsgASkf+UNu/T8+mgqDHwG3KZDC9tjzVQBx9VQV3WyCnUeTYQ/5IoyJYE/9/";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqVcP3W9xDjyrxQpwPiE1p0BO+YUOdqY+FkVYirafbk7YAcVoM2jCE052BKLAcgV9FQKn0mJyfQcQbngYXdveH+/aADFssw6+HKu8WPfZhk8LSYsVSyb/omCDPoEwvzbVhA/1ys1T9B/nDIhtBH320TKHOjnUqjvX5Ff/MnnF6Wvu4z1m/HXx8LOpPmdRgKDPT7zATdrcG/Lf2LMANVmD/2YST5r/yOQCTnwlJe7Rzcdom3wXiqJOySrzaw5nb+/3PLkaflFICQkOWHrPJIHizgvBCrKWi8luW2qXCPGDqmFToLB4QqnHK8kioOs612ln0KOOyXHJMBxJP7HenkKrZwIDAQAB";

        //可设置异步通知接收服务地址（可选） 需要设置，并且是一个外网能访问的域名，当然也可以使用内网穿透
        config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";

        return config;
    }
}